//index.js
//获取应用实例
const app = getApp()
var ble_master = require('../ble/ble_master.js');

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),

    // feederDeviceName: "AUTO_FDERv",
  },
  //事件处理函数
  bindViewTap: function() {
    // wx.navigateTo({
    //   url: '../logs/logs'
    // })
    // if(ble_master.data.ble_device) {
    //   this.christp_jumpToAutoPlantPage()
    // } else {
    //   // Bluetooth master
    //   // ble_master.ble_onDeviceFound(this.christp_foundBluetoothDeviceCallback)
    //   // ble_master.ble_openAdaptorandStartDiscovery()
    // }
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
      // console.log("Get user info 1")
      this.bindViewTap()
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
        // console.log("Get user info 2")
        this.bindViewTap()
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
          // console.log("Get user info 3")
          this.bindViewTap()
        }
      })
    }
    // this.bindViewTap()

    // console.log(this.data.feederDeviceName.substring(0, this.data.feederDeviceName.length))
    // this.planb_go()
    // this.temperatue_go()
    
    
  },

  plana_go: function (params) {
    wx.navigateTo({
      url: '../y_plan/y_plan_main'
    })
  },

  planb_go: function (params) {
    wx.navigateTo({
      url: '../y_plan/y_plan_planb'
    })
  },

  temperatue_go: function (params) {
    wx.navigateTo({
      url: '../body_temp/id_check'
    })
  },

  // christp_afterBluetoothConnectDevice() {
  //   // Should jump to another page
  //   console.log("Connect finished")
  // },

  christopherGoAutoPlant() {
    wx.navigateTo({
      url: '../autoplant/main_page'
    })
  },

  christopherGoAutoFeeder() {
    wx.navigateTo({
      url: '../autofeeder/main_page'
    })
  },

  bleToolGo() {
    wx.navigateTo({
      url: '../bletool/mainTool_page'
    })
  },



  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
    // console.log("Get user info 4")
    this.bindViewTap()
  }
})
